package com.insurance.common.utils;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class NumberUtils {
	
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

	private static String PROPOSAL_PREFIX = "p";

	private static String ORDER_PREFIX = "p";

	/**
	 * 随机数
	 * @param number 位数
	 * @return
	 */
	public static String randomNumber(int number){
		Random random = new Random();
	    StringBuffer sb = new StringBuffer();
	    for(int i = 0; i < number ; i++){
	    	sb.append(random.nextInt(9)+1);
	    }
	    return sb.toString();
	}

	/**
	 * 生成投保单号
	 *
	 * @param productCode
	 * @return
	 * @throws Exception
	 */
	public static String generateProposalNo(String productCode) throws Exception {
		if(StringUtils.isBlank(productCode)){
			throw new Exception("【生成投保单号失败, 产品编码为空】");
		}
		return PROPOSAL_PREFIX + productCode + dateFormat.format(new Date()) + randomNumber(6);
	}

	/**
	 * 生成订单号
	 *
	 * @param productCode
	 * @return
	 * @throws Exception
	 */
	public static String generateOrderNo(String productCode) throws Exception {
		if(StringUtils.isBlank(productCode)){
			throw new Exception("【生成订单号失败, 产品编码为空】");
		}
		return ORDER_PREFIX + productCode + dateFormat.format(new Date()) + randomNumber(6);
	}
	
}
